package model
{
	import flash.data.SQLResult;
	import flash.data.SQLStatement;
	import flash.events.SQLErrorEvent;
	import flash.events.SQLEvent;
	import flash.net.Responder;
	
	import mx.controls.Alert;
	import mx.controls.DataGrid;
	
	
	public class Suppliers extends Inventory
	{
		[Bindable] public var arr_result:Array;
		[Bindable] private var _dataProvider:Array;
		private static const CREATE_SUPPLIERS:String =
			"CREATE TABLE IF NOT EXISTS suppliers (" + 
				"pk BIGINT PRIMARY KEY, " + 
				"id TEXT, name TEXT, tin TEXT, address TEXT, " + 
				"contact TEXT, contact_number TEXT)";
		
		/**
		 * creates table suppliers if does not exist
		 */
		public function Suppliers()
		{
			super();
			func_create_table(CREATE_SUPPLIERS);
			func_queryall();
		}
		
		public function set dataProvider(results:Array):void {
			_dataProvider = results;
		}
		
		public function get dataProvider():Array {
			return _dataProvider;
		}
		
		/**
		 * create new supplier 
		*/
		public function func_new(param_supplier:Supplier):void {
			var sql_statement:SQLStatement = new SQLStatement();
			sql_statement.sqlConnection = sql_conn;
			param_supplier.func_set_sqlstatement(sql_statement,param_supplier.s_insert_statement);
			sql_statement.execute();
		} 
		
		/**
		 * delete supplier 
		*/
		public function func_delete_row(param_supplier:Supplier):void {
			var sql_statement:SQLStatement = new SQLStatement();
			sql_statement.sqlConnection = sql_conn;
			param_supplier.func_set_sqlstatement(sql_statement,param_supplier.s_delete_statement);
			sql_statement.execute();
		}
		
		/**
		 * update supplier 
		*/
		public function func_update_row(param_supplier:Supplier):void {
			var sql_statement:SQLStatement = new SQLStatement();
			sql_statement.sqlConnection = sql_conn;
			param_supplier.func_set_sqlstatement(sql_statement,param_supplier.s_update_statement);
			sql_statement.execute();
		}
		
		/**
		 * retrieve all suppliers
		*/
		public function func_queryall():void {
			var sql_statement:SQLStatement = new SQLStatement();
			sql_statement.sqlConnection = sql_conn;
			sql_statement.text = "SELECT * FROM suppliers";
			sql_statement.itemClass = Supplier;
			sql_statement.execute();
			func_set_results(sql_statement.getResult());
		}
		
		/**
		 * retrieve some suppliers
		*/
		public function func_querysome(args:Array):void {
			var sql_statement:SQLStatement = new SQLStatement();
			sql_statement.sqlConnection = sql_conn;
			var s_where:String = "";
			for (var item:* in args) {
				if (s_where == "") s_where += " WHERE ";
				else {
					s_where += " AND ";
				}
				if (item == "id")  {
					s_where += item + " LIKE :" + item;
					sql_statement.parameters[':'+item] = "%" + args[item] + "%";
				}
				
				if (item == "name")  {
					s_where += item + " LIKE :" + item;
					sql_statement.parameters[':'+item] = "%" + args[item] + "%";
				}
				
				if (item == "tin") {
					s_where += item + " = :" + item;
					sql_statement.parameters[':'+item] = args[item];
				}
				
			}
			sql_statement.text = "SELECT * FROM suppliers" + s_where;
			sql_statement.itemClass = Supplier;
			sql_statement.execute();
			func_set_results(sql_statement.getResult());
		} 
		private function func_set_results(result:SQLResult):void {
			if (result.data != null) {
				dataProvider = result.data;
			} else dataProvider = [];
		}
	}
}